<?php

namespace Admin\Controller; //当前命名空间跟文件目录一样
use Common\Common\CommonController;  //继承公共控制器类，时刻检测管理员身份
class UserController extends CommonController
{

	//显示会员列表
	public function showList()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		$model = D('user');
		$userInfo = $model->field('user_id,u_name,u_gender')->relation(false)->select();
		$this->assign("userInfo", $userInfo);
		$this->assign("a", '11');
		$this->display();
	}

	//查询会员信息
	public function query()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//获取传递的用户名称信息
		$userName = I('user_name');
		$where = array('u_name' => array("like", '%' . $userName . '%')); //构建用户名称模糊查询条件
		$model = D('user');   //实例化模型类对象	
		$userInfo = $model->where($where)->select();  //获取模糊查询会员信息
		$this->assign("userInfo", $userInfo);
		$this->display();
	}

	//修改会员信息
	public function update()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//获取User模型类对象
		$model = D('User');
		if (IS_POST) //有表单提交
		{
			//create()接收表单数据
			$user_info = $model->create();

			//进行修改信息检查
			$error[] = checkPhone($user_info['mobile_phone']);
			$error[] = checkEmail($user_info['u_email']);
			//没有格式错误信息
			if (empty(implode($error))) {
				//进行数据更新，失败提示错误并放回个人信息列表
				if ($model->where(array("user_id" => $user_info['user_id']))->save($user_info) !== false) {
					$this->success("恭喜您，修改成功！！！", U('?m=admin&c=user&a=showList'));
					die;
				}
				$this->error("出现未知错误！！！", U('?m=admin&c=user&a=showList'));
				die;
			}
			$this->error("手机或邮箱格式有误，修改失败！！！", U('?m=admin&c=user&a=showList'));
			die;
		}
		//获取传递的客户id通过会话信息查询用户个人信息
		$user_id = I('uid');
		$where = array('user_id' => $user_id);
		$field = 'user_id,u_name,u_gender,mobile_phone,u_email,u_address,u_birthday'; //获取字段字符串
		$user_info = $model->field($field)->where($where)->find();
		$this->assign('user_info', $user_info);
		$this->display();
	}

	//删除会员信息
	public function  deleteUser()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//接收删除的会员编号
		$user_id = I('uid');
		$model = D('User');
		if ($model->where(array('user_id' => $user_id))->delete()) {
			//使用关联，删除该用户下的所有订单
			$model->relation(true)->where(array('user_id' => $user_id))->delete();
			$this->success("会员所有信息删除成功", U('?m=admin&c=user&a=showList'));
			die;
		}
		$this->error("会员信息删除失败", U('?m=admin&c=user&a=showList'));
	}

	//查看会员个人订单
	public function personOrder()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//获取当前用户id
		$user_id = I('uid');
		$this->assign('user_id', $user_id);
		//实例化用户关联模型
		$modelUser = D('User');
		//接收传递订单状态查询值os
		$shipping_status = I('os') != null ? I('os') : '';
		$shipping_status = ($shipping_status == 1) ? '待发货' : $shipping_status;
		$shipping_status = ($shipping_status == 2) ? '待收货' : $shipping_status;
		$shipping_status = ($shipping_status == 3) ? '已收货' : $shipping_status;

		if (!empty($shipping_status)) {
			$where = array('shipping_status' => $shipping_status);
			//实例化订单模型，查询目前用户具体物流状态订单
			$orderInfo = M('Order')->where(array('user_id' => $user_id))->where($where)->select();
			//将订单数据返回到视图
			$this->assign("orderInfo", $orderInfo);
		} else {
			// print_r($where);die;
			//模型类对象获取当前用户的全部订单信息
			$orderInfo = $modelUser->relation('Order')->where(array('user_id' => $user_id))->select()[0]['Order'];
			//将订单数据返回到视图
			$this->assign("orderInfo", $orderInfo);
		}
		$this->display();
	}
}
